﻿using System.Linq;
using FluentValidation;
using netCoreStudy.Dto.User;
using netCoreStudy.IService;

namespace netCoreStudy.DtoValidator
{
	public class AddUserValidator : AbstractValidator<AddUserDto>
	{
		public AddUserValidator(IUserService userService, IRoleService roleService, IMaterialService materialService)
		{
			RuleFor(x => x.Name)
				.NotNull()
				.NotEmpty()
				.WithName("用户姓名")
				.WithMessage("{PropertyName}不能为空!")
				.Must(x => !userService.ExistByName(x))
				.WithMessage("{PropertyName}已存在!")
				;
			RuleFor(x => x.RoleIds)
				.NotNull()
				.WithName("角色id")
				.WithMessage("{PropertyName}不能为空!")
				.Must(x => x.Any())
				.WithMessage("{PropertyName}至少选择一个角色!")
				.Must(x => x.All(r => roleService.ExistById(r)))
				.WithMessage("{PropertyName}中其中某个角色不存在!")
				;
		}
	}
}